const express = require("express");
let router = express.Router();

const DB = require("../utils/db完整.js");
const { api_url } = require("../config/config.js");

router.get("/courseinfo", async (req, res, next) => {
    //cid是唯一标识这个课程的参数
    let { cid } = req.query;
    //查询课程基本信息
    let sql = `select id,cid,title,price,intro, concat("${api_url}","/uploads/","videoDetail/",detail_imgs) as detail_imgs,area_name,subject_name,category_name,tid from e_course where cid="${cid}"`;
    let [err, result] = await DB(sql);
    console.log(result);
    if (err) {
        next("courseinfo请求失败");
        return
    }
    // console.log(result);
    //当result里面有数据就说明找到了，空就是找不到
    if (result.length == 0) {
        next("找不到此课程");
        return
    }

    //查询老师表
    //结构result里面的tid
    let [{ tid }] = result;
    let sql1 = `select id,intro,realname,tid,concat("${api_url}","/uploads/","videodetailUser/",head_photo_url) as head_photo_url from e_teacher where tid="${tid}"`;
    let [err1, result1] = await DB(sql1);
    if (err1) {
        next("老师信息请求失败");
        return
    }
    //视频列表
    let sql2 = `select id,cid,vnum,video_title from e_video where cid="${cid}"`;
    let [err2, result2] = await DB(sql2);
    if (err2) {
        next("视频信息请求失败");
        return
    }
    res.send({
        status: 200,
        msg: "课程信息请求成功",
        result: {
            course: result[0],//课程信息
            teacherinfo: result1[0],//老师信息
            video_list: result2//视频列表
        }
    })
})


module.exports = router;